<?php

class WebUser extends CWebUser
{
    public $message;
    protected $cart, $model, $id, $name;

    public function init()
    {
        Yii::import('application.models.account.Customer');
        $this->cart=Yii::app()->cart;
        $this->model=new Customer();
        parent::init();
    }

    public function getCart()
    {
        return $this->cart;
    }

    public function getId()
    {
        if(!$this->id){
            return $this->id=$this->getState('id');
        }
        return $this->id;
    }

    public function getName()
    {
        if(!$this->name){
            return $this->id=$this->getState('name');
        }
        return $this->name;
    }

    public function getModel()
    {
        if(!$this->isGuest){
            return $this->model=$this->model->findByPk($this->getState('id'));
        }
        return $this->model;
    }

    public function loginUser($identity)
    {
        $name=$identity->username;
        $password=$identity->password;
        $model=$this->model->findByAttributes(array('email'=>$name));

        if($model===null || $model->password!==Yii::app()->salt->getHash($password.$model->email)){
            $this->message=Yii::t('account/customer', 'errorNoSuchUser');
            return false;
        }else{
            $this->id=$model->id;
            $this->name=$model->firstname;

            $this->setState('id',$this->id);
            $this->setState('name',$this->name);

            $this->model=$model;
            return $this->login($identity);
        }
    }
}